<script>
import {axiosGetPersonInfo, axiosNewPosts} from "../api";

export default {
  name: "newPost",
  data(){
    return{
      username:"",
      title:"",
      text:"",
      imgList:[],
      fileList: [],
      dialogVisible:false,
      dialogInfo:"发布成功"
    }
  },
  beforeMount() {
    this.getPersonInfo();
  },
  methods: {
    async getPersonInfo() {
      let result = await axiosGetPersonInfo({account: this.account, password: this.password});
      if (result.status === 200) {
        this.username = result.data.userName;
      }
    },
    back(){
      this.$router.push({name:"publicPosts"});
    },
    newPosts(){
      axiosNewPosts({title:this.title,account:this.account,username:this.username,text:this.text});
      this.dialogVisible=true;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    handleEnter(){
      this.dialogVisible=false;
      this.$router.push({name:"publicPosts"});
    }
  }
}
</script>

<template>
  <div id="newPost">
    <el-dialog
        :before-close="handleClose"
        :visible.sync="dialogVisible"
        title="提示"
        width="30%">
      <span>{{dialogInfo}}</span>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="handleEnter">确 定</el-button>
  </span>
    </el-dialog>
    <div class="middle-box">
      <div class="title">
        <el-button @click="back">返回</el-button>
        <p>发布我的新帖</p>
      </div>
      <div class="post">
        <el-input v-model="title" placeholder="请输入标题" style="margin-bottom: 20px;"></el-input>
        <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入内容"
            v-model="text">
        </el-input>
        <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </div>
      <div class="btn">
        <el-button @click="newPosts()">发布</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped src="../style/new-post.css">

</style>